package com.ruoyi.tongji.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gongjulei;
import com.ruoyi.jinchuguanli.domain.ShangPinCun;
import com.ruoyi.jinchuguanli.domain.Shangpin;
import com.ruoyi.jinchuguanli.mapper.GouhuorukuMapper;
import com.ruoyi.jinchuguanli.mapper.XiaoshouchukuMapper;
import com.ruoyi.tongji.domain.*;
import com.ruoyi.tongji.mapper.JieCunMapper;
import com.ruoyi.tongji.service.IJieCunService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class IJieCunServiceImpl implements IJieCunService {

    @Autowired
    private JieCunMapper jieCunMapper;

    @Autowired
    private ShangpinguanliMapper shangpinguanliMapper;

    @Override
    public TableDataInfo selectJieCunList(String beginXiadanriqi, String endXiadanriqi, int pageNum, int pageSize) throws ParseException {

        PageInfo<GuiGeSelect> guiGeSelectPageInfo = PageUtils.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> jieCunMapper.selectGuiGeList());
        PageInfo<jieCunDan> jieCunDanPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(guiGeSelectPageInfo, jieCunDanPageInfo);

        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        if (StringUtils.isNotEmpty(beginXiadanriqi)) {
            startDate = new java.sql.Date(ft.parse(beginXiadanriqi).getTime());
        }
        if (StringUtils.isNotEmpty(endXiadanriqi)) {
            endDate = new java.sql.Date(ft.parse(endXiadanriqi).getTime());
        }

        List<GuiGeSelect> geSelects = guiGeSelectPageInfo.getList();
        List<jieCunDan> danList = new ArrayList<>();
        List<ShangPinXiangQing> chukuShangpinList = jieCunMapper.selectShangPinChuRuXiangQing(startDate, endDate);
        List<ShangPinXiangQing> rukuShangpinList = jieCunMapper.selectShangPinRuXiangQing(startDate, endDate);
        List<JieCunShangPin> chukuList = new ArrayList<>();
        List<JieCunShangPin> rukuList = new ArrayList<>();
        gongjulei.aFormat(chukuShangpinList, chukuList, shangpinguanliMapper);
        gongjulei.aFormat(rukuShangpinList, rukuList, shangpinguanliMapper);
        for (GuiGeSelect ggs : geSelects
        ) {
            List<JieCunShangPin> danChukuList = new ArrayList<>();
            List<JieCunShangPin> danRukuList = new ArrayList<>();
            jieCunDan jieCunDan = new jieCunDan();
            BeanUtils.copyProperties(ggs, jieCunDan);
            jieCunDan.setShangpinkucun(ggs.getKucunshuliang());
            List<Shangpin> jieCunShangPinList = jieCunMapper.selectShangPinList(ggs);
            List<JieCun> jieCunList = new ArrayList<>();
            List<String> bianhaoList = new ArrayList<>();
            for (Shangpin sp : jieCunShangPinList
            ) {
                JieCun jieCun = new JieCun();
                BeanUtils.copyProperties(sp, jieCun);
                jieCun.setKucunshuliang(Math.toIntExact(sp.getShangpinshuliang()));
                bianhaoList.add(sp.getShangpinbianhao());
                jieCunList.add(jieCun);
            }
            jieCunDan.setJieCunList(jieCunList);

            gongjulei.danAddShangpin(rukuList, danRukuList, bianhaoList, ggs);
            gongjulei.danAddShangpin(chukuList, danChukuList, bianhaoList, ggs);
            jieCunDan.setGCCunList(danRukuList);
            jieCunDan.setXCCunList(danChukuList);
            danList.add(jieCunDan);
        }


        jieCunDanPageInfo.setList(danList);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(danList);
        rspData.setTotal(jieCunDanPageInfo.getTotal());

        return rspData;
    }

    @Override
    public List<jieCunDan> selectjieCunDan(String beginXiadanriqi, String endXiadanriqi) throws ParseException {
        List<GuiGeSelect> geSelects = jieCunMapper.selectGuiGeList();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        if (StringUtils.isNotEmpty(beginXiadanriqi)) {
            startDate = new java.sql.Date(ft.parse(beginXiadanriqi).getTime());
        }
        if (StringUtils.isNotEmpty(endXiadanriqi)) {
            endDate = new java.sql.Date(ft.parse(endXiadanriqi).getTime());
        }

        List<jieCunDan> danList = new ArrayList<>();
        List<ShangPinXiangQing> chukuShangpinList = jieCunMapper.selectShangPinChuRuXiangQing(startDate, endDate);
        List<ShangPinXiangQing> rukuShangpinList = jieCunMapper.selectShangPinRuXiangQing(startDate, endDate);
        List<JieCunShangPin> chukuList = new ArrayList<>();
        List<JieCunShangPin> rukuList = new ArrayList<>();
        gongjulei.aFormat(chukuShangpinList, chukuList, shangpinguanliMapper);
        gongjulei.aFormat(rukuShangpinList, rukuList, shangpinguanliMapper);
        for (GuiGeSelect ggs : geSelects
        ) {
            List<JieCunShangPin> danChukuList = new ArrayList<>();
            List<JieCunShangPin> danRukuList = new ArrayList<>();
            jieCunDan jieCunDan = new jieCunDan();
            BeanUtils.copyProperties(ggs, jieCunDan);
            jieCunDan.setShangpinkucun(ggs.getKucunshuliang());
            List<Shangpin> jieCunShangPinList = jieCunMapper.selectShangPinList(ggs);
            List<JieCun> jieCunList = new ArrayList<>();
            List<String> bianhaoList = new ArrayList<>();
            for (Shangpin sp : jieCunShangPinList
            ) {
                JieCun jieCun = new JieCun();
                BeanUtils.copyProperties(sp, jieCun);
                jieCun.setKucunshuliang(Math.toIntExact(sp.getShangpinshuliang()));
                bianhaoList.add(sp.getShangpinbianhao());
                jieCunList.add(jieCun);
            }
            jieCunDan.setJieCunList(jieCunList);

            gongjulei.danAddShangpin(rukuList, danRukuList, bianhaoList, ggs);
            gongjulei.danAddShangpin(chukuList, danChukuList, bianhaoList, ggs);
            jieCunDan.setGCCunList(danRukuList);
            jieCunDan.setXCCunList(danChukuList);
            danList.add(jieCunDan);
        }
        return danList;
    }

    @Override
    public TableDataInfo selectQiTaList(Integer i, int pageNum, int pageSize) {
        PageInfo<RequestQiTaTianJia> qiTaTianJiaPageInfo = PageUtils.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> jieCunMapper.selectQiTaList(i));
        PageInfo<QiTaTianJiaReturn> qiTaTianJiaReturnPageInfo = new PageInfo<>();

        if (i == 1) {
            BeanUtils.copyProperties(qiTaTianJiaPageInfo, qiTaTianJiaReturnPageInfo);

            List<RequestQiTaTianJia> qiTaTianJiaList = qiTaTianJiaPageInfo.getList();
            List<QiTaTianJiaReturn> returnList = new ArrayList<>();

            if (qiTaTianJiaList.size() > 0) {
                if (StringUtils.isNotEmpty(qiTaTianJiaList.get(0).getShangpinbianhao())) {
                    for (RequestQiTaTianJia rqt : qiTaTianJiaList
                    ) {
                        QiTaTianJiaReturn qiTaTianJiaReturn = new QiTaTianJiaReturn();
                        BeanUtils.copyProperties(rqt, qiTaTianJiaReturn);
                        List<ShangPinCun> shangpinCunList = JSONArray.parseArray(rqt.getShangpinbianhao(), ShangPinCun.class);
                        List<Shangpin> shangpinList = new ArrayList<>();
                        for (ShangPinCun spc:shangpinCunList
                             ) {
                            Shangpin shangpin = gongjulei.shangpinFormat(spc,shangpinguanliMapper);
                            shangpinList.add(shangpin);
                        }
                        qiTaTianJiaReturn.setShangpinList(shangpinList);
                        returnList.add(qiTaTianJiaReturn);
                    }
                }
                qiTaTianJiaReturnPageInfo.setList(returnList);
            }

            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(0);
            rspData.setRows(returnList);
            rspData.setTotal(qiTaTianJiaReturnPageInfo.getTotal());

            return rspData;
        } else {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(0);
            rspData.setRows(qiTaTianJiaPageInfo.getList());
            rspData.setTotal(qiTaTianJiaPageInfo.getTotal());

            return rspData;
        }
    }

    @Override
    public int insertRequestQiTaTianJia(String id, RequestQiTaTianJia rqt) throws ParseException {
        if ("1".equals(id)) {
            List<Shangpin> shangpinList = JSONArray.parseArray(rqt.getShangpinbianhao(),Shangpin.class);
            List<ShangPinCun> shangPinCunList = new ArrayList<>();
            for (Shangpin sp:shangpinList
                 ) {
                ShangPinCun spc = new ShangPinCun();
                spc.setShangpinbianhao(sp.getShangpinbianhao());
                spc.setShangpinshuliang(sp.getShangpinshuliang());
                shangPinCunList.add(spc);
            }
            String str = JSONObject.toJSONString(shangPinCunList);
            rqt.setShangpinbianhao(str);
        }
        rqt.setType(Integer.valueOf(id));

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(sf.parse(rqt.getDateForString()).getTime());
        rqt.setDateFor(date);
        return jieCunMapper.insertRequestQiTaTianJia(rqt);
    }

    @Override
    public QiTaTianJiaReturn selectQiTaTianJia(Long id) {
        RequestQiTaTianJia qiTaTianJia = jieCunMapper.selectRequestQiTaTianJia(id);
        QiTaTianJiaReturn qiTaTianJiaReturn = new QiTaTianJiaReturn();
        BeanUtils.copyProperties(qiTaTianJia,qiTaTianJiaReturn);
        if (StringUtils.isNotEmpty(qiTaTianJia.getShangpinbianhao())) {
            List<ShangPinCun> shangpinCunList = JSONArray.parseArray(qiTaTianJia.getShangpinbianhao(), ShangPinCun.class);
            List<Shangpin> shangpinList = new ArrayList<>();
            for (ShangPinCun spc:shangpinCunList
            ) {
                Shangpin shangpin = gongjulei.shangpinFormat(spc,shangpinguanliMapper);
                shangpinList.add(shangpin);
            }
            qiTaTianJiaReturn.setShangpinList(shangpinList);
        }
        return qiTaTianJiaReturn;
    }

    @Override
    public int updateQiTaTianJia(String id, RequestQiTaTianJia qiTaTianJia) throws ParseException {
        if ("1".equals(id)) {
            List<Shangpin> shangpinList = JSONArray.parseArray(qiTaTianJia.getShangpinbianhao(),Shangpin.class);
            List<ShangPinCun> cunList = new ArrayList<>();
            for (Shangpin sp:shangpinList
                 ) {
                ShangPinCun shangPinCun = new ShangPinCun();
                shangPinCun.setShangpinbianhao(sp.getShangpinbianhao());
                shangPinCun.setShangpinshuliang(sp.getShangpinshuliang());
                cunList.add(shangPinCun);
            }
            String str = JSONObject.toJSONString(cunList);
            qiTaTianJia.setShangpinbianhao(str);
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(sf.parse(qiTaTianJia.getDateForString()).getTime());
        qiTaTianJia.setDateFor(date);
        return jieCunMapper.updateQiTaTianJia(qiTaTianJia);
    }

    @Override
    public int deleteRequestQiTaTianJiaByIds(String ids) {
        String[] strings = Convert.toStrArray(ids);
        return jieCunMapper.deleteRequestQiTaTianJiaByIds(strings);
    }
}
